package kayao.client.network.message;

import kayao.client.data.SharedMemory;
import kayao.client.network.SynchronizedTime;
import kayao.client.scene.objects.CarNode;
import kayao.common.Client;
import kayao.common.KayaoValues;
import kayao.common.NetworkUtils;

public class StateUpdateMessage extends Message{

	private static final int RETRIES = 1;
	private static final int TIMEOUT_LENGTH_MILLIS = 1000;
	private static final int PRIORITY = PRIORITY_HIGH;
	
	public StateUpdateMessage(String targetAddress) {
		super(targetAddress, RETRIES, TIMEOUT_LENGTH_MILLIS, PRIORITY);
		createPacketData();
	}
	@Override
	public String type() {
		// TODO Auto-generated method stub
		return "StateUpdate";
	}
	@Override
	public void createPacketData() {
		// Magic number (int32)+ Message id (b)+ Message counter (int32)+ Client id (int32)+
		// Translation x (float32)+ Translation z (float32)+ Rotation y(float32)+
		// Velocity x(float32) + Velocity z(float32)
		// Length = 4+1+4+4+4+4+4+4+4 = 33
		
		mSendData = new byte[37];
		int index = 0;

		// Insert magic number (int32)
		NetworkUtils.insertIntToByteArray(mSendData, index, KayaoValues.MAGIC_NUMBER);
		index += 4;
		// Insert message id (b)
		mSendData[index] = KayaoValues.STATE_UPDATE;
		index++;
		//Insert message counter (int32)
		NetworkUtils.insertIntToByteArray(mSendData, index, mMessageId);
		index += 4;
		// Insert local client id (int32)
		NetworkUtils.insertIntToByteArray(mSendData, index, SharedMemory.getInstance().getLocalId());
		index += 4;
		// Insert synchronized time (int32)
		NetworkUtils.insertIntToByteArray(mSendData, index, 
				SynchronizedTime.getTime());
		index += 4;
		// Insert translation x (float32)
		NetworkUtils.insertFloatToByteArray(mSendData, index, 
				SharedMemory.getInstance().getAvatar().getTranslationX());
		index += 4;
		// Insert translation z (float32)
		NetworkUtils.insertFloatToByteArray(mSendData, index, 
				SharedMemory.getInstance().getAvatar().getTranslationZ());
		index += 4;
		// Insert rotation y (float32)
		NetworkUtils.insertFloatToByteArray(mSendData, index, 
				SharedMemory.getInstance().getAvatar().getRotationY());
		index += 4;
		// Insert velocity x (float32)
		NetworkUtils.insertFloatToByteArray(mSendData, index, 
				SharedMemory.getInstance().getAvatar().getVelocityX());
		index += 4;
		// Insert velocity z (float32)
		NetworkUtils.insertFloatToByteArray(mSendData, index, 
				SharedMemory.getInstance().getAvatar().getVelocityZ());
		index += 4;
		
	}
	
	public static void processPacketData(byte[] data){
		
		int index = 9; // Previous data already used (Magic number + message id + message counter)

		// Get client id from which message was sent
		int clientId = NetworkUtils.insertByteArrayToInt(data, index);
		index += 4;
		// Get client id from which message was sent
		int syncTime = NetworkUtils.insertByteArrayToInt(data, index);
		index += 4;
		// Get translation x from sending client
		float translationX = NetworkUtils.insertByteArrayToFloat(data, index);
		index += 4;
		// Get translation z from sending client
		float translationZ = NetworkUtils.insertByteArrayToFloat(data, index);
		index += 4;
		// Get rotation y from sending client
		float rotationY = NetworkUtils.insertByteArrayToFloat(data, index);
		index += 4;
		// Get velocity x from sending client
		float velocityX = NetworkUtils.insertByteArrayToFloat(data, index);
		index += 4;
		// Get velocity from sending client
		float velocityZ = NetworkUtils.insertByteArrayToFloat(data, index);
		index += 4;
		
		Client client = SharedMemory.getInstance().getClientById(clientId);
		if (client != null) {
			System.out.println("[Message] \t Got a State Update from player: " + client.getName());
			CarNode car = SharedMemory.getInstance().getCarById(clientId);
			if (car == null) {
				car = new CarNode(client);
				car.setTranslation(translationX, 0, translationZ);
				car.setRotationY(rotationY);
				car.setVelocityX(velocityX);
				car.setVelocityZ(velocityZ);
				SharedMemory.getInstance().addEntity(car);
			} else {
				car.setTranslation(translationX, 0, translationZ);
				car.setRotationY(rotationY);
				car.setVelocityX(velocityX);
				car.setVelocityZ(velocityZ);
			}
		}
	}

}
